<?php

require_once 'feedSession.php';
require_once 'tblTicker.php';

class FeedManager{
	private $session;
	private $tableTicker;
	private $tableProvider;
	private $provider;
	
	static $nInstance = 0;
	static $objInstance = NULL;
	
	public static function Create(){
		if(FeedManager::$nInstance == 0){
			FeedManager::$nInstance++;
			$objInstance = new FeedManager();
		}
		return $objInstance;
	}
	
	public static function Destroy(){
		if(FeedManager::$nInstance > 0){
			FeedManager::$nInstance--;
			if(FeedManager::$nInstance == 0)
				unset($objInstance);
		}
	}
	
	private function __construct(){
		$this->session = new FeedSession();
		$this->tableTicker = new tblTicker();
		$this->tableProvider = new TblProvider();
	}
	
	private function __destruct(){
		unset($this->provider);
		unset($this->session);
		unset($this->tableProvider);
		unset($this->tableTicker);
	}
	
	/**
	 * carica UNA company alla volta: occorre richiamarla per ogni company
	 */
	public function FeedAllStockData(){
		if($this->session->IsRunning() == FALSE){
			if($this->session->IsWaitingForStart() == TRUE){
				//nuova sessione
				$this->session->Start();
				$ts = time();
				$skipRecord = 0;
				$selCompany = $this->tableTicker->GetFirstCompany();
			}
			else{
				//riprendi l'ultima sessione
				$this->session->Resume();
				$ts = $this->session->GetCurrent()->timestamp;
				$skipRecord = $this->session->GetCurrent()->skipRecord;
				$selCompany = $this->tableTicker->GetCompanyByName($this->session->GetCurrent()->companyName);
			}
	
			//cerca il miglior provider disponibile
			$provRank = 0;
			do{
				$provRank++;
				$this->provider = $this->tableProvider->GetProviderByRank($provRank);
				$coTicker = $selCompany->GetTicker($this->provider->GetName());
				$provq = $this->provider->Query($coTicker, $ts);
			}while((isset($provq) == FALSE) && isset($this->provider));

			if(isset($this->provider)){
				//riempie la tabella dei dati della company selezionata
				if($this->provider->GetType() == Provider::$TYPE_CONDENSEDTICK)
					$stockDataTable = new tblStockDataEx($coTicker->GetName());
				else
					$stockDataTable = new tblStockData($coTicker->GetName());

				$loadData = TRUE;
				$skipRecord = 0;
				while($loadData && $this->session->TimerElapsed() == FALSE){
					$rawData = $provq->GetRow($skipRecord++);
					if(isset($rawData)){
						$currStockData = $this->provider->Generate($rawData);
						$stockDataTable->Insert($currStockData);
					}
					else
						$loadData = FALSE;
				}

				if($this->session->TimerElapsed() == FALSE)
					//terminato il caricamento dei dati del timestamp selezionato:
					//imposta il giorno precedente
					$this->session->GetCurrent()->timestamp = $ts - (24*60*60);
				else
					$this->session->GetCurrent()->skipRecord = $skipRecord;
				$this->session->Pause();
			}
			else{
				$this->session->GetCurrent()->companyName = $this->tableTicker->GetNextCompanyByName($coTicker->GetName());
				$this->session->End();
			}
		}
	}

	public function GetCurrentSession(){
		return $this->session;
	}
}

?>
